package com.tianma315.web.merchant.controller;


import java.io.IOException;
import java.util.Arrays;


import com.tianma315.core.common.utils.Result;
import com.tianma315.core.merchant.domain.MerchantDO;
import com.tianma315.core.merchant.service.MerchantService;
import com.tianma315.core.merchant.vo.MerchantVO;
import com.tianma315.core.oss.service.FileService;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


/**
 * 
 * <pre>
 * 商户表
 * </pre>
 * <small> 2019-08-01 09:20:13 | gwl</small>
 */
@Controller
@RequestMapping("/merchant/merchant")
public class MerchantController extends BaseController {
	@Autowired
	private MerchantService merchantService;

	@Autowired
	private FileService fileService;
	
	@GetMapping()
	@RequiresPermissions("merchant:merchant:merchant")
	String Merchant(){
	    return "merchant/merchant/merchant";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("merchant:merchant:merchant")
	public Result<Page<MerchantDO>> list(Integer pageNumber, Integer pageSize, MerchantDO merchantDTO){
		// 查询列表数据
        Page<MerchantDO> page = merchantService.getMerchantDOPage(pageNumber,pageSize,merchantDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("merchant:merchant:add")
	String add(){
	    return "merchant/merchant/add";
	}

	@GetMapping("/edit/{merchantId}")
	@RequiresPermissions("merchant:merchant:edit")
	String edit(@PathVariable("merchantId") Long merchantId,Model model){
		MerchantDO merchant = merchantService.selectById(merchantId);
		model.addAttribute("merchant", merchant);
	    return "merchant/merchant/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("merchant:merchant:add")
	public Result<String> save(MerchantVO merchant){
		try {
			if(merchantService.insertMerchant(merchant)){
                return Result.ok();
            }
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("merchant:merchant:edit")
	public Result<String>  update( MerchantDO merchant){
		merchantService.updateById(merchant);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("merchant:merchant:remove")
	public Result<String>  remove( Long merchantId){
		if(merchantService.deleteMerchantService(merchantId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("merchant:merchant:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] merchantIds){
//		merchantService.deleteBatchIds(Arrays.asList(merchantIds));
		for (Long id:merchantIds) {
			merchantService.deleteMerchantService(id);
		}
		return Result.ok();
	}


	/**
	 * 删除
	 */
	@PostMapping( "/removeInfo")
	@ResponseBody
	@RequiresPermissions("merchant:merchant:remove")
	public Result<String>  removeInfo( Long merchantId){
		if(merchantService.removeInfo(merchantId)){
			return Result.ok();
		}
		return Result.fail();
	}

}
